package integration;

import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import style1.*;
import style2.*;
import style2.Alphabetizer;
import style2.Input;
import style2.Output;
import style2.Shift;
import style3.*;
import style4.*;

public class RUN {
	public static void main(String[] args){
		JFrame frame = new JFrame("范昊天的软件体系结构教学软件");
		frame.setSize(1600, 1000);
		frame.setLayout(null);
		frame.setLocationRelativeTo(null);
		frame.setBackground(Color.red);

		JTabbedPane tab = new JTabbedPane();
	
		tab.add("主程序-子程序",createPanel(1));
		tab.add("面向对象",createPanel(2));
		tab.add("事件系统",createPanel(3));
		tab.add("管道-过滤",createPanel(4));

		tab.setSelectedIndex(0);

		frame.setContentPane(tab);
		frame.setVisible(true);
	}
	private static JComponent createPanel(int type) {
		JPanel panel = new JPanel();
		panel.setBackground(Color.lightGray);
		panel.setLayout(new GridBagLayout());
		String content_text = new String();
		String content_imgurl = new String();
		String content_code = new String();
		if(type == 1) {
			content_text = "主程序/子程序风格的主要实现机制是模块实现,它将每个子程序都实现为一个模块,\n 主程序实现为整个系统的起始模块(在很多语言中，即为包含main函数的模块)\n依照抽象规格的层次关系，实现模块也被组织为相应的层次机构，\n通过导入/导出关系相连接"
					+ "需要强调的是，虽然主程序/子程序风格非常类似于结构化程序的结构,\n但是主程序/子程序风格是基于部件与连接件建立的高层结构。"
					+ "它的部件不同于程序,\n而是更加粗粒度的模块。而且，在部件的实现模块内部,可以使用结构化分析方法。\n ";
					
			content_imgurl = "images and other files/style1.png";
			content_code = "核心代码shift类:\r\n"
					+ "public void shift() {\r\n"
					+ "        Iterator<String> it = lineTxt.iterator();\r\n"
					+ "        while (it.hasNext()) {\r\n"
					+ "        StringTokenizer token = new StringTokenizer(it.next());\r\n"
					+ "        ArrayList<String> tokens = new ArrayList<String>();\r\n"
					+ "        int i = 0;\r\n"
					+ "        int count = token.countTokens();\r\n"
					+ "        while (i < count) {\r\n"
					+ "        tokens.add(token.nextToken());\r\n"
					+ "        i++;\r\n"
					+ "    }";
		}
		else if(type == 2) {
			content_text = "面向对象风格：数据和作用在数据上的操作被封装为object，主程序调用这些object，形成控制流程。\n"
					+ "数据不再被构件直接共享，而是被封装在object中，每个对象提供了一个接口，允许其他对象通过改接口调用对该对象内封装的数据的操作。";
			content_imgurl = "images and other files/style2.png";
			content_code = "核心代码:\r\n"
					+ "public static void main(String[] args) {\r\n"
					+ "        Input input = new Input();\r\n"
					+ "        input.input(\"images and other files/input.txt\");\r\n"
					+ "        Shift shift = new Shift(input.getLineTxt());\r\n"
					+ "        shift.shift();\r\n"
					+ "        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());\r\n"
					+ "        alphabetizer.sort();\r\n"
					+ "        Output output = new Output(alphabetizer.getKwicList());\r\n"
					+ "        output.output(\"images and other files/output.txt\");\r\n"
					+ "    }";
		}
		else if(type == 3) {
			content_text = "基于事件系统的风格：基于事件通知的系统中的各个功能并不是直接被调用的，\n"
					+ "而是通过组建通知或广播事件信息而触发的。其他组件可以通过注册一个与某个事件通知相关联的过程而与其发生联系。";
			content_imgurl = "images and other files/style3.png";
			content_code = "核心代码:\r\n"
					+ "public static void main(String[] args) {\r\n"
					+ "        //创建主题\r\n"
					+ "        KWICSubject kwicSubject = new KWICSubject();\r\n"
					+ "        //创建观察者\r\n"
					+ "        Input input = new Input(\"images and other files/input.txt\");\r\n"
					+ "        Shift shift = new Shift(input.getLineTxt());\r\n"
					+ "        Alphabetizer alphabetizer = new Alphabetizer(shift.getKwicList());\r\n"
					+ "        Output output = new Output(alphabetizer.getKwicList(), \"images and other files/output.txt\");\r\n"
					+ "\r\n"
					+ "        // 将观察者加入主题\r\n"
					+ "        kwicSubject.addObserver(input);\r\n"
					+ "        kwicSubject.addObserver(shift);\r\n"
					+ "        kwicSubject.addObserver(alphabetizer);\r\n"
					+ "        kwicSubject.addObserver(output);\r\n"
					+ "        // 逐步调用各个观察者\r\n"
					+ "        kwicSubject.startKWIC();\r\n"
					+ "    }";
		}
		else if(type == 4) {
			content_text = "管道-过滤软件体系结构：四个过滤器：输入，移位，排序，输出，每个过滤器处理数据，\n然后将结果送至下一个过滤器，控制器是分布式的，只要有数据传入，过滤器即开始工作，过滤器之间的数据共享被严格限制在管道传输。\n"
					+ "输入过滤器从数据源读取输入文件，解析格式，将行写入输出管道。\n"
					+ "对象之间没有直接耦合，对比主程序风格但是有很多复杂的数据依赖，对比面向对象风格有对象依赖和间接依赖关系。";
			content_imgurl = "images and other files/style4.png";
			content_code = "核心代码:\r\n"
					+ "public static void main(String[] args) throws IOException {\r\n"
					+ "        File inFile = new File(\"images and other files/input.txt\");\r\n"
					+ "        File outFile = new File(\"images and other files/output.txt\");\r\n"
					+ "        Pipe pipe1 = new Pipe();\r\n"
					+ "        Pipe pipe2 = new Pipe();\r\n"
					+ "        Pipe pipe3 = new Pipe();\r\n"
					+ "        Input input = new Input(inFile, pipe1);\r\n"
					+ "        Shift shift = new Shift(pipe1, pipe2);\r\n"
					+ "        Alphabetizer alphabetizer  = new Alphabetizer(pipe2, pipe3);\r\n"
					+ "        Output output = new Output(outFile,pipe3);\r\n"
					+ "        input.transform();\r\n"
					+ "        shift.transform();\r\n"
					+ "        alphabetizer.transform();\r\n"
					+ "        output.transform();\r\n"
					+ "    }";
		}
		else {
			return null;
		}
		
		Font font = new Font(Font.SANS_SERIF, Font.BOLD, 15);
		JTextArea desc = new JTextArea(content_text);
		desc.setFont(font);
		JScrollPane scroll_desc = new JScrollPane(desc);
		scroll_desc.setFont(new Font(null, Font.PLAIN, 50));
		GridBagConstraints gridBagConstraints_1 = new GridBagConstraints();
		gridBagConstraints_1.gridy = 0;
		gridBagConstraints_1.gridx = 0;
		gridBagConstraints_1.weightx = 0.1;
		gridBagConstraints_1.weighty = 0.4;
		gridBagConstraints_1.fill = GridBagConstraints.BOTH;
		panel.add(scroll_desc,gridBagConstraints_1);
		
		
		ImageIcon img = new ImageIcon(content_imgurl);
		img.setImage(img.getImage().getScaledInstance(500, 240, 0));
		JLabel label = new JLabel(img);
		GridBagConstraints gridBagConstraints_2 = new GridBagConstraints();
		gridBagConstraints_2.gridy = 0;
		gridBagConstraints_2.gridx = 1;

		gridBagConstraints_2.weightx = 0.2;
		gridBagConstraints_2.weighty = 0.4;
		gridBagConstraints_2.fill = GridBagConstraints.HORIZONTAL;
		panel.add(label,gridBagConstraints_2);
		
		JTextArea result = new JTextArea("运行结果");
		result.setFont(font);
		JScrollPane scroll_result = new JScrollPane(result);
		scroll_result.setFont(new Font(null, Font.HANGING_BASELINE, 15));
		GridBagConstraints gridBagConstraints_5 = new GridBagConstraints();
		gridBagConstraints_5.gridy = 2;
		gridBagConstraints_5.gridx = 0;
		gridBagConstraints_5.gridheight = 2;
		gridBagConstraints_5.weightx = 0.4;
		gridBagConstraints_5.weighty = 1;
		gridBagConstraints_5.fill = GridBagConstraints.BOTH;
		panel.add(scroll_result,gridBagConstraints_5);
		
		JTextArea code = new JTextArea(content_code);
		code.setFont(font);
		JScrollPane scroll_code = new JScrollPane(code);
		scroll_result.setFont(new Font(null, Font.PLAIN, 10));
		GridBagConstraints gridBagConstraints_3 = new GridBagConstraints();
		gridBagConstraints_3.gridy = 1;//行
		gridBagConstraints_3.gridx = 0;//列
		gridBagConstraints_3.weightx = 0.5;
		gridBagConstraints_3.weighty = 0.5;
		gridBagConstraints_3.fill = GridBagConstraints.BOTH;
		panel.add(scroll_code,gridBagConstraints_3);
		
		JButton button = new JButton("RUN");
		GridBagConstraints gridBagConstraints_4 = new GridBagConstraints();
		button.setFont(new Font(null, Font.BOLD, 30));
		button.setForeground(Color.red);
		button.setBackground(Color.gray);
		gridBagConstraints_4.gridy = 2;//行
		gridBagConstraints_4.gridx = 1;//列
		gridBagConstraints_4.weightx = 0.1;
		gridBagConstraints_4.weighty = 0.5;
		gridBagConstraints_4.ipadx = 200;
		gridBagConstraints_4.ipady = 100;
		panel.add(button,gridBagConstraints_4);
	
		if(type == 1) {
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						result.setText("主程序-子程序演示运行结果：");
						clearFile();
						style1.demo1.main(null);
						String result_content = getFileContent();
						result.append(result_content);
					}
					catch (Exception e1) {
			            e1.printStackTrace();
			        }
				}
			});
		}
		else if(type == 2) {
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						result.setText("面向对象演示运行结果：");
						clearFile();
						style2.Main.main(null);
						String result_content = getFileContent();
						result.append(result_content);
					}
					catch (Exception e1) {
			            e1.printStackTrace();
			        }
				}
			});
		}
		else if(type == 3) {
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						result.setText("事件系统-观察者模式演示运行结果：");
						clearFile();
						style3.Main.main(null);
						String result_content = getFileContent();
						result.append(result_content);
					}
					catch (Exception e1) {
			            e1.printStackTrace();
			        }
				}
			});
		}
		else if(type == 4) {
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						result.setText("管道-过滤演示运行结果：");
						clearFile();
						style4.Main.main(null);
						String result_content = getFileContent();
						result.append(result_content);
					}
					catch (Exception e1) {
			            e1.printStackTrace();
			        }
				}
			});
		}
		else {
			return null;
		}
		
		return panel;
	}
	public static String getFileContent(){
		File file = new File("images and other files/output.txt");
	    StringBuilder result = new StringBuilder();
	    try{
	      BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
	      String res = null;
	      while((res = bufferedreader.readLine())!=null){
	        result.append(System.lineSeparator()+res);
	      }
	      bufferedreader.close();  
	    }catch(Exception e){
	      e.printStackTrace();
	    }
	    return result.toString();
	  }
	public static void clearFile(){
		File file = new File("images and other files/output.txt");
		FileWriter filewriter;
		try {
			filewriter = new FileWriter (file);
			filewriter.write("");
			filewriter.flush();
			filewriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
}